package com.github.skystardust.lotteryex.commands;

import com.github.skystardust.lotteryex.LotteryEX;
import com.github.skystardust.lotteryex.config.RewardConfig;
import com.github.skystardust.lotteryex.lottery.LotteryItem;
import com.github.skystardust.lotteryex.lottery.bundle.AwardBundle;
import com.github.skystardust.lotteryex.lottery.bundle.LotteryBundle;
import com.github.skystardust.lotteryex.lottery.bundle.RequireBundle;
import com.github.skystardust.lotteryex.lottery.bundle.profile.LotteryProfile;
import com.github.skystardust.lotteryex.lottery.item.imp.award.AwardItemItem;
import com.github.skystardust.lotteryex.lottery.item.imp.require.RequireItemItem;
import com.github.skystardust.lotteryex.utils.OptionalUtils;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;

import java.io.IOException;
import java.util.*;
import java.util.function.Supplier;

public class LotteryRunCommand implements CommandExecutor {
    private static Object random(Class o) {
        Object[] enumConstants = o.getEnumConstants();
        int i1 = new Random().nextInt(enumConstants.length);
        for (int i = 0; i < o.getEnumConstants().length; i++) {
            if (i == i1) {
                return enumConstants[i];
            }
        }
        return null;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        /*Inventory inventory = Bukkit.createInventory(null, InventoryType.ENDER_CHEST, "LotteryEX");
        TreeMap<Integer, ItemStack> itemStackIntegerTreeMap = LotteryEX.getLotteryEX()
                .getRewardConfig()
                .getLotteryItemList()
                .get(0)
                .getDisplayMap();
        LotteryAnimation.newBuilder()
                .withCurrentLotteryInventory(inventory)
                .withLotteryInventoryOwner(((Player) sender).getUniqueId())
                .withInventoryUpdateRunnable(AnimationRegularIncreaseRunnable.newBuilder()
                        .withAwardItemStackMap(itemStackIntegerTreeMap)
                        .withMiscItemStackMap(new TreeMap<>())
                        .withInventory(inventory)
                        .build())
                .build();
        ((Player) sender).openInventory(inventory);*/
        if (args.length == 1) {//todo debug config ,remove in release
            if (args[0].equalsIgnoreCase("run")) {
                RewardConfig rewardConfig = LotteryEX.getLotteryEX().getRewardConfig();
                List<AwardBundle> awardItemItemList = new ArrayList<>();
                for (int i = 0; i < 100; i++) {
                    AwardBundle build = AwardBundle.newBuilder()
                            .withAwardItemList(
                                    Collections.singletonList(
                                            new AwardItemItem(new ItemStack(Material.GOLD_AXE))
                                    )
                            )
                            .withUuid(UUID.randomUUID())
                            .build();
                    awardItemItemList.add(build);
                }
                LotteryItem build = LotteryItem.newBuilder()
                        .withAwardBundleList(awardItemItemList)
                        .withRequireBundleList(Collections.singletonList(
                                RequireBundle.newBuilder()
                                        .withUuid(UUID.randomUUID())
                                        .withRequireItemList(
                                                Collections.singletonList(
                                                        new RequireItemItem(
                                                                new ItemStack(Material.GOLD_AXE)
                                                        )
                                                )
                                        )
                                        .withUuid(UUID.randomUUID())
                                        .build()
                        ))
                        .withDisplayItemStack(new ItemStack(Material.GOLD_AXE))
                        .withUuid(UUID.randomUUID())
                        .build();
                Supplier<Map<LotteryItem, LotteryProfile>> mapSupplier = () -> {
                    HashMap<LotteryItem, LotteryProfile> stringObjectHashMap = new HashMap<>();
                    stringObjectHashMap.put(build, LotteryProfile.newBuilder()
                            .withChance(10)
                            .withSpecChancePermissionMap(new OptionalUtils<>(() -> {
                                HashMap<String, Double> objectObjectHashMap = new HashMap<>();
                                objectObjectHashMap.put("LotteryEX.lottery.bypass", 100.0D);
                                return objectObjectHashMap;
                            }).getOptional().orElseGet(HashMap::new))
                            .build());
                    return stringObjectHashMap;
                };
                LotteryBundle lotteryBundle = LotteryBundle.newBuilder()
                        .withLotteryItemProfileMap(mapSupplier.get())
                        .withUuid(UUID.randomUUID())
                        .build();
                rewardConfig.getFileConfiguration().set("test", lotteryBundle);
                try {
                    rewardConfig.getFileConfiguration().save(rewardConfig.getFile());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
}
